package logic

import (
	"context"
	"errors"
	"srv/models"

	"srv/house"
	"srv/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type HouseImageLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewHouseImageLogic(ctx context.Context, svcCtx *svc.ServiceContext) *HouseImageLogic {
	return &HouseImageLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *HouseImageLogic) HouseImage(in *house.HouseImageRequest) (*house.HouseImageResponse, error) {
	// todo: add your logic here and delete this line
	var newUser models.User
	err := newUser.FirstUser(int(in.UserId))
	if err != nil {
		logx.Error("用户未登录")
		return nil, errors.New("用户未登录")
	}
	var newHouse models.House
	err = newHouse.FirstHouse(int(in.HouseId))
	if err != nil {
		logx.Error("房源不存在")
		return nil, errors.New("房源不存在")
	}
	image := models.HouseImage{
		HouseId:   int(in.HouseId),
		ImageName: in.ImageName,
		Image:     in.Image,
	}
	err = image.CreateHouseImage()
	if err != nil {
		logx.Error("房源图片添加失败")
		return nil, errors.New("房源图片添加失败")
	}
	return &house.HouseImageResponse{
		HouseImageId: int64(image.ID),
	}, nil
}
